package com.tjshop.android.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.tjshop.android.R;
import com.tjshop.android.model.tj.json.CartModel;
import com.tjshop.android.util.ImageLoaderUtils;
import java.util.List;
import java.util.Map;

/**
 * Created by gytaiSSD on 2014/12/29.
 */
public class CartGoodsListAdapter extends BaseAdapter implements OnCheckedChangeListener {
	private LayoutInflater inflater;
	List<CartModel> cartModels;
	private Context mContext;
	private Handler mHandler;
	private boolean[] selected;
	private int mode = 1;
	public CartGoodsListAdapter(Context context, List<CartModel> clist,
			LayoutInflater inflater,Handler mHandler, boolean[] selected2) {
		this.inflater = inflater;
		cartModels = clist;
		this.mContext = context;
		this.mHandler = mHandler;
		this.selected = selected2;
	}

	@Override
	public int getCount() {
		return cartModels.size();
	}

	@Override
	public Object getItem(int i) {
		return cartModels.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(final int i, View convertView, ViewGroup viewGroup) {
		  ViewHolder viewHolder = null;
	        if (null == convertView)
	        {
	            viewHolder = new ViewHolder();
	            convertView = inflater.inflate(
						R.layout.inc_activity_main_fragment_cart_list_item, null);

	            viewHolder.name = (TextView) convertView.findViewById(R.id.item_title);
	            viewHolder.item_icon = (ImageView) convertView
	                    .findViewById(R.id.item_icon);
	            viewHolder.num = (TextView) convertView.findViewById(R.id.num);
	            viewHolder.price = (TextView) convertView.findViewById(R.id.item_ori_pre_price);
	            viewHolder.select = (CheckBox) convertView.findViewById(R.id.select);
	            viewHolder.add = (Button) convertView.findViewById(R.id.numadd);
	            viewHolder.disadd = (Button) convertView.findViewById(R.id.numdiscrease);
	            viewHolder.num_edit = (TextView) convertView.findViewById(R.id.num_edit);
	            viewHolder.edit_mode = (RelativeLayout) convertView.findViewById(R.id.rela_edit);
	            convertView.setTag(viewHolder);
	        }
	        else
	        {
	            viewHolder = (ViewHolder) convertView.getTag();
	        }
	        final ViewHolder vh = viewHolder;
	        
	        if(this.mode == 1){
	        	viewHolder.edit_mode.setVisibility(View.GONE);
	        }else{
	        	viewHolder.edit_mode.setVisibility(View.VISIBLE);
	        }

	        CartModel c = cartModels.get(i);
	        if(viewHolder.item_icon.getTag() == null || viewHolder.item_icon.equals("") || !viewHolder.item_icon.getTag().equals(c.getThumb())){
	        	ImageLoaderUtils.getInstance(mContext).displayImage(c.getThumb(), viewHolder.item_icon);
	        	viewHolder.item_icon.setTag(c.getThumb());
	        }
	        
	        viewHolder.name.setText(c.getGname());
	        
	        viewHolder.price.setText(c.getPrice()+"");
	        
	        viewHolder.num.setText("数量:   "+c.getNum());
	        viewHolder.num_edit.setText(c.getNum()+"");
	        if(selected[i]){
	        	viewHolder.select.setChecked(true);
	        }else{
	        	viewHolder.select.setChecked(false);
	        }
	        viewHolder.select.setTag(i);
	        
	        viewHolder.add.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					String num = vh.num_edit.getText().toString();
					int n = Integer.parseInt(num);
					vh.num_edit.setText(n+1+"");
					Message msg = new Message();
					msg.obj =  i;
					msg.arg1 = n+1;
					msg.what = 7;
					mHandler.sendMessage(msg);
					
				}
			});
	        viewHolder.disadd.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					String num = vh.num_edit.getText().toString();
					int n = Integer.parseInt(num);
					if(n == 0){
						return;
					}
					vh.num_edit.setText(n-1+"");
					Message msg = new Message();
					msg.obj =  i;
					msg.arg1 = n-1;
					msg.what = 8;
					mHandler.sendMessage(msg);
				}
			});
	        
	        viewHolder.select.setOnCheckedChangeListener(this);
	        
	        return convertView;
	}
	private static class ViewHolder
    {
        CheckBox select;
        ImageView item_icon;
        TextView name;
        TextView price;
        TextView num;
        RelativeLayout edit_mode;
        Button add,disadd;
        TextView num_edit;
    }
	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		Message msg = new Message();
		msg.obj =  arg0.getTag();
		if(arg1){
			msg.what = 5;
		}else{
			msg.what = 6;
		}
		mHandler.sendMessage(msg);
	}

	public void setMode(int i) {
		this.mode = i;
	}

	public void setSelected(boolean[] selected2) {
		this.selected = selected2;
	}
	
}
